//
//  HomeImageFilterView.swift
//  WatermarkCamera
//
//  Created by Python on 2024/7/26.
//

import UIKit
import RxRelay
import SnapKit

class HomeImageFilterView: BaseView {

    enum ContrastButtonEvent {
        case down
        case up
    }
    
    private lazy var wrapperView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()

    /// stackView
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 0.0
        stackV.distribution = .fill
        stackV.alignment = .fill
        return stackV
    }()
    private lazy var titleView: UIView = {
        let view = UIView()
        return view
    }()
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = 15.axc.uiFont(weight: .medium)
        label.textColor = UIColor.black85
        label.text = "滤镜设置"
        return label
    }()
    private lazy var closeBtn: UIButton = {
        let button = UIButton()
        button.setImage("close_12".axc.uiImage, for: .normal)
        return button
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lineColor
        return view
    }()
    
    private lazy var contentView: UIView = {
        let view = UIView()
        return view
    }()
    private lazy var contentStackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 12.0
        stackV.distribution = .fill
        stackV.alignment = .fill
        return stackV
    }()
    
    private lazy var contrastView: UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var contrastBtn: UIButton = {
        let button = UIButton()
        button.setTitle("长按此处对比色差", for: .normal)
        button.setTitleColor(UIColor.black85, for: .normal)
        button.titleLabel?.font = 13.axc.uiFont
        button.setImage("changan_bijiao".axc.uiImage, for: .normal)
        return button
    }()
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 44.0, height: collectionH - 1.0)
        layout.minimumLineSpacing = 16.0
        let collectionV = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionV.backgroundColor = UIColor.clear
        collectionV.showsHorizontalScrollIndicator = false
        collectionV.delegate = self
        collectionV.dataSource = self
        collectionV.register(cellClass: HomeImageFilterCell.self)
        return collectionV
    }()
    
    /// 滤镜数据源
    var items: [ImageFilterItem] = [] {
        didSet {
            collectionView.reloadData()
        }
    }
    
    /// 隐藏状态
    private var _hidden: Bool = false
    
    /// wrapperView顶部约束
    var wrapperViewTopConstraint: Constraint?
    
    private let collectionH = 72.0
    /// 点击关闭
    let closeAction: PublishRelay<Void> = .init()
    /// 长按/取消长按对比
    let contrastAction: PublishRelay<ContrastButtonEvent> = .init()
    /// 选中某个滤镜效果回调
    let didSelecteFilterItem: PublishRelay<ImageFilterItem> = .init()
    
    // MARK: UI
    override func createUI() {
        super.createUI()
        let hMargin = 16.0
        let vMargin = 12.0
        let titleVH = 44.0
        let closeBtnW = 24.0
        let contrastBtnW = 140.0
        let contrastBtnH = 40.0
        backgroundColor = UIColor.clear
    
        // wrapperView
        addSubview(wrapperView)
        wrapperView.snp.makeConstraints { make in
            self.wrapperViewTopConstraint = make.top.equalTo(self.snp.bottom).constraint
            make.left.right.bottom.equalToSuperview()
        }

        // stackView
        wrapperView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(-Global.safeBottom - 20.0)
        }
        // 标题view
        stackView.addArrangedSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.height.equalTo(titleVH)
        }
        // 关闭按钮
        titleView.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { make in
            make.right.equalTo(-hMargin)
            make.top.bottom.equalToSuperview()
            make.width.equalTo(closeBtnW)
        }
        // 标题label
        titleView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(hMargin)
            make.centerY.equalToSuperview()
            make.right.equalTo(closeBtn.snp.left).offset(-hMargin)
        }
        // 分割线
        stackView.addArrangedSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.height.equalTo(0.5)
        }
        // 内容
        stackView.addArrangedSubview(contentView)
        // 内容stackView
        contentView.addSubview(contentStackView)
        contentStackView.snp.makeConstraints { make in
            make.top.equalTo(vMargin)
            make.bottom.equalToSuperview()
            make.left.equalTo(hMargin)
            make.right.equalTo(-hMargin)
        }
        // 对比view
        contentStackView.addArrangedSubview(contrastView)
        // 对比按钮
        contrastView.addSubview(contrastBtn)
        contrastBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.height.equalTo(contrastBtnH)
            make.width.equalTo(contrastBtnW)
            make.centerY.equalToSuperview()
        }
        // 滤镜view
        contentStackView.addArrangedSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.height.equalTo(collectionH)
        }

    }
    
    override func bindForwardAction() {
        super.bindForwardAction()
        // 点击关闭按钮
        closeBtn.rx.tap.bind(to: closeAction).disposed(by: rx.disposeBag)
        // 按下对比按钮
        contrastBtn.axc.addEvent(.touchDown) {
            [weak self] _ in
            guard let self = self else { return }
            self.contrastAction.accept(.down)
        }
        // 抬起对比按钮
        contrastBtn.axc.addEvent(.touchUpInside) {
            [weak self] _ in
            guard let self = self else { return }
            self.contrastAction.accept(.up)
        }
    }

}


extension HomeImageFilterView {
    
    /// 隐藏或显示
    func setHidden(_ hidden: Bool) {
        guard _hidden != hidden else { return }
        _hidden = hidden
        wrapperViewTopConstraint?.deactivate()
        if hidden { // 隐藏
            wrapperView.snp.makeConstraints { make in
                self.wrapperViewTopConstraint = make.top.equalTo(self.snp.bottom).constraint
            }
        } else { // 显示
            self.isHidden = false
            wrapperView.snp.makeConstraints { make in
                self.wrapperViewTopConstraint = make.top.equalToSuperview().constraint
            }
        }
        UIView.animate(withDuration: 0.25) {
            self.layoutIfNeeded()
        } completion: { _ in
            if hidden {
                self.isHidden = true
            }
        }
    }
    
    /// 刷新列表
    func reloadData() {
        collectionView.reloadData()
    }
}


// MARK: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
extension HomeImageFilterView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item = items[indexPath.item]
        let cell = collectionView.dequeueReusableCell(cellClass: HomeImageFilterCell.self, for: indexPath)
        cell.item = item
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let item = items[indexPath.item]
        if item.isSelected { return }
        // 回调
        didSelecteFilterItem.accept(item)
    }
}

